<template>
  <p-modal
    ref="reference"
    v-model="visible"
    :width="1200"
    title="新增"
    @ok="handleOk"
    @cancel="cancel('ruleForm')"
  >
    <p-form-model
      ref="ruleForm"
      :model="form"
      :rules="rules"
      @ok="handleSubmit"
    >
      <p-row>
        <p-col :span="16">
          <p-form-model-item
            ref="snPn"
            label="SN/PN"
            prop="snPn"
            :labelCol="labelCol2"
            :wrapperCol="wrapperCol2"
          >
            <p-input
              placeholder="SN/PN"
              v-model="form.snPn"
              @pressEnter="pressEnter"
            />
          </p-form-model-item>
        </p-col>
        <p-col :span="8">
          <p-form-model-item
            ref="linename"
            label="送修线体"
            prop="linename"
            :labelCol="labelCol3"
            :wrapperCol="wrapperCol3"
          >
            <p-input v-model="form.linename" />
          </p-form-model-item>
        </p-col>
        <p-col :span="8">
          <p-form-model-item
            label="产品编码"
            prop="mcode"
            :labelCol="labelCol"
            :wrapperCol="wrapperCol"
            ref="mcode"
          >
            <p-input
              v-model="form.mcode"
              @change="changeinput"
            />
          </p-form-model-item>
        </p-col>
        <p-col :span="8">
          <p-form-model-item
            ref="mname"
            label="产品名称"
            prop="mname"
            :labelCol="labelCol"
            :wrapperCol="wrapperCol"
          >
            <p-input v-model="form.mname" />
          </p-form-model-item>
        </p-col>
        <p-col :span="8">
          <p-form-model-item
            ref="serviceNumber"
            label="维修数量"
            prop="serviceNumber"
            :labelCol="labelCol"
            :wrapperCol="wrapperCol"
          >
            <p-input v-model="form.serviceNumber" />
          </p-form-model-item>
        </p-col>
        <p-col :span="8">
          <p-form-model-item
            ref="processTwoname"
            label="工序名称"
            prop="processTwoname"
            :labelCol="labelCol"
            :wrapperCol="wrapperCol"
          >
            <p-input v-model="form.processTwoname" />
          </p-form-model-item>
        </p-col>
        <p-col
          :span="8"
          :offset="8"
          :pull="8"
        >
          <p-form-model-item
            ref="stationName"
            label="工站名称"
            prop="stationName"
            :labelCol="labelCol"
            :wrapperCol="wrapperCol"
          >
            <p-input v-model="form.stationName" />
          </p-form-model-item>
        </p-col>
        <p-col :span="8">
          <p-form-model-item
            ref="productionno"
            label="生产工单"
            prop="productionno"
            :labelCol="labelCol"
            :wrapperCol="wrapperCol"
          >
            <p-input v-model="form.productionno" />
          </p-form-model-item>
        </p-col>
        <p-col :span="8">
          <p-form-model-item
            ref="processbillno"
            label="工序工单"
            prop="processbillno"
            :labelCol="labelCol"
            :wrapperCol="wrapperCol"
          >
            <p-input v-model="form.processbillno" />
          </p-form-model-item>
        </p-col>
        <p-col :span="8">
          <p-form-model-item
            ref="classtimename"
            label="所属班组"
            prop="classtimename"
            :labelCol="labelCol"
            :wrapperCol="wrapperCol"
          >
            <p-input v-model="form.classtimename" />
          </p-form-model-item>
        </p-col>
        <p-col :span="8">
          <p-form-model-item
            ref="bigProblem"
            label="问题大类"
            prop="bigProblem"
            :labelCol="labelCol"
            :wrapperCol="wrapperCol"
          >
            <p-input v-model="form.bigProblem" />
          </p-form-model-item>
        </p-col>
        <p-col
          :span="8"
          :offset="8"
          :pull="8"
        >
          <p-form-model-item
            ref="smallProblem"
            label="问题小类"
            prop="smallProblem"
            :labelCol="labelCol"
            :wrapperCol="wrapperCol"
          >
            <p-tree-select
              @change="changeDefect()"
              v-model="form.smallProblem"
              :tree-data="secondStations"
              :dropdown-style="{maxHeight: '500px'}"
              tree-node-filter-prop="title"
              show-search
              allow-clear
              tree-default-expand-all
            />
          </p-form-model-item>
        </p-col>
        <p-col :span="24">
          <p-form-model-item
            ref="description"
            label="问题描述"
            prop="description"
            :labelCol="labelCol4"
            :wrapperCol="wrapperCol4"
          >
            <p-textarea v-model="form.description" />
          </p-form-model-item>
        </p-col>
        <p-col :span="8">
          <p-form-model-item
            ref="receiptNumber"
            label="单据编号"
            prop="receiptNumber"
            :labelCol="labelCol"
            :wrapperCol="wrapperCol"
          >
            <p-input v-model="form.receiptNumber" />
          </p-form-model-item>
        </p-col>
        <p-col
          :span="8"
          :offset="8"
        >
          <p-form-model-item
            ref="sendRepairDate"
            label="送修日期"
            prop="sendRepairDate"
            :labelCol="labelCol"
            :wrapperCol="wrapperCol"
          >
            <p-input v-model="form.sendRepairDate" />
          </p-form-model-item>
        </p-col>
      </p-row>
    </p-form-model>
  </p-modal>
</template>
<script>
// import  JDictSelectUtil from "@/components/dict/JDictSelectUtil";
import {
  postAction,
  getAction,
  deleteAction,
  putAction,
  api
} from "@/api/manage";
// import Order from './components/order'
import mesPeProcessApi from "@/api/mesPeProcess";
import { timestampToDatetime } from "@/utils/util";
export default {
  // props: ["getDefect"],
  components: {
    // JDictSelectUtil
  },
  data() {
    return {
      visible: false,
      labelCol: {
        // 标签布局
        span: 6 //栅格占位格数
      },
      wrapperCol: {
        // 控件布局
        span: 15
      },
      labelCol2: {
        span: 3
      },
      wrapperCol2: {
        span: 20
      },
      labelCol3: {
        span: 6
      },
      wrapperCol3: {
        span: 15
      },
      labelCol4: {
        span: 2
      },
      wrapperCol4: {
        span: 21
      },

      // 下拉选择
      barCode: "",
      // 下拉工序工站
      params: {
        processTwoCode: "", //父级需要传给后台的
        lineno: "", // 渲染到页面上的
        stationCode: "" // 子集需要传给后台的
      },
      processTree: [], //下拉工序工站整个数据

      // 下拉问题大小类
      // 小类
      barCode1: "",
      processTree1: [],

      form: {
        badCode: "",
        defectProjectName: "",
        // defectCode: "",

        snPn: "", //sn码
        linename: "", // 线体名称
        mcode: "", // 产品编码
        mname: "", // 产品名称
        serviceNumber: "", // 维修数量   无
        processTwoname: "", // 工序名称  无
        stationName: "", // 工站名称  无
        productionno: "", // 生产工单
        processbillno: "", // 工序工单
        description: "", // 问题描述   无
        receiptNumber: "", // 单据编号
        sendRepairDate: "", //送修日期
        classtimename: "", //所属班组  无
        bigProblem: "", // 问题大类   无
        smallProblem: "" // 问题小类  无
      },
      // 验证规则
      rules: {
        snPn: [
          { required: true, message: "请输入正确的SN/PN码", trigger: "blur" }
        ],
        linename: [
          { required: true, message: "请输入正确的送修线体", trigger: "blur" }
        ],
        mcode: [
          { required: true, message: "请输入正确的产品编码", trigger: "blur" }
        ],
        mname: [
          { required: true, message: "请输入正确的产品名称", trigger: "blur" }
        ],
        serviceNumber: [
          { required: true, message: "请输入正确的维修数量", trigger: "blur" }
        ],
        processTwoname: [
          {
            required: true,
            message: "请输入正确的工序名称",
            trigger: "blur"
          }
        ],
        stationName: [
          {
            required: true,
            message: "请输入正确的工站名称",
            trigger: "blur"
          }
        ],
        productionno: [{ required: true, message: "请输入正确的生产工单" }],
        processbillno: [
          { required: true, message: "请输入正确的工序工单", trigger: "blur" }
        ],
        description: [
          { required: true, message: "请输入正确的问题描述", trigger: "blur" }
        ],
        receiptNumber: [
          { required: true, message: "请输入正确的单据编号", trigger: "blur" }
        ],
        sendRepairDate: [
          { required: true, message: "请输入正确的送修日期", trigger: "blur" }
        ],
        classtimename: [
          { required: true, message: "请输入正确的所属班组", trigger: "blur" }
        ],
        bigProblem: [
          { required: true, message: "请输入正确的送修日期", trigger: "blur" }
        ],
        smallProblem: [
          { required: true, message: "请输入正确的问题小类", trigger: "change" }
        ]
      },
      // 请求地址
      url: {
        // 新增
        add: "/mes-manufacture/mfOnlineRepairTask/addWithPriority",
        // 获取新增的sn码和 自动带出信息
        getSn: "/mes-manufacture/mfOnlineRepairTask/getInfo",
        // 获取工序名称和编码
        getprocessBillno: "/mes-craft/craft/mesPeDefinition/search/tree",
        // 新增 问题大小类
        defect: "/mes-quality/mesQcDefectProjectMaintain/selectByBadGroupType/1"
      },
      // dragIn: {}
      formCode: {}
    };
  },
  computed: {
    qisParams() {
      const barCode1 = this.barCode1;
      const smallProblem = this.form.smallProblem;
      return {
        barCode1,
        smallProblem
      };
    },
    secondStations() {
      const data = [];
      this.processTree1.forEach(item => {
        const children = [];
        item.defectProjectVoList.forEach(child => {
          children.push({
            title: child.defectProjectName,
            value: child.defectProjectCode
          });
        });
        data.push({
          disabled: true,
          title: item.badGroupName,
          value: item.badGroupCode,
          children
        });
      });

      return data;
    }
  },
  created() {
    this.getStations();

    // this.getRuleCodeValue();
  },
  methods: {
    getStations() {
      // 获取 问题大小类
      getAction(this.url.defect).then(res => {
        this.processTree1 = res.data || [];
      });
    },

    // 根据问题小类获取大类
    changeDefect() {
      this.processTree1.forEach(el => {
        el.defectProjectVoList.forEach(ele => {
          if (ele.defectProjectCode == this.form.smallProblem) {
            // console.log(el.badGroupName); // 父级el
            this.form.bigProblem = el.badGroupName;
          }
        });
      });
      if (typeof this.form.smallProblem === "undefined") {
        this.form.bigProblem = "";
      }
    },

    // 根据sn码获取基本信息
    pressEnter() {
      const formData = {
        sn: this.form.snPn
      };
      getAction(this.url.getSn, formData).then(res => {
        if (res.code == 0) {
          this.formCode = res.data;
          const forms = this.form;
          for (var key in forms) {
            for (var key1 in res.data) {
              if (key == key1) {
                forms[key] = res.data[key1];
              }
            }
          }
          this.form.serviceNumber = 1;
        }
        this.getRuleCodeValue();
      });
      // console.log(this.form.snPn);
    },

    // 获取单据编号 (在基础数据中的编码规则中生成)
    getRuleCodeValue() {
      let param = {
        commonFormData: {},
        rules: [
          {
            ruleCode: "mes_send_a_single_number",
            columnName: "labelTemplateNo"
          }
        ]
      };
      putAction(api.ruleBaseURL, param).then(res => {
        // 执行成功，获取返回的值，并赋到页面上
        if (res.success) {
          console.log("生成的编码：" + res.data);
          const item = res.data;
          this.form.receiptNumber = item.labelTemplateNo;
          // this.form.setFieldsValue(pick(item, Object.keys(item)));
        }
      });

      // 获取时间
      var timestamp = new Date().getTime();
      const time = timestampToDatetime(timestamp, 2);
      this.form.sendRepairDate = time;
    },

    // 重置表单
    cancel(formName) {
      this.$refs[formName].resetFields(() => {});
    },
    // 表单提交
    handleOk() {
      // 提交再次验证
      this.$refs.ruleForm.validate(valid => {
        if (valid) {
          const formData = {
            snPn: this.form.snPn, // sn码
            linename: this.form.linename, // 送修线体
            mcode: this.form.mcode, // 产品编码
            mname: this.form.mname, // 产品名称
            productionno: this.form.productionno, // 生产工单
            processBillno: this.form.processbillno, // 工序工单---
            receiptNumber: this.form.receiptNumber, // 单据编号
            sendRepairDate: this.form.sendRepairDate, // 送修日期
            sendRepairProcess: this.formCode.processTwocode, // 工序名称
            sendRepairStation: this.formCode.stationCode, // 工站名称
            remark: this.form.description, // 问题描述
            smallFault: this.form.smallProblem, // 问题小类
            bigFault: this.form.bigProblem, // 问题大类
            repairNumber: this.form.serviceNumber, //维修数量
            classtimeno: this.formCode.workroomno, // 班组编号
            classtimename: this.form.classtimename // 所属班组
          };
          postAction(this.url.add, formData).then(res => {
            if (res.code == 0) {
              this.$message.success("新增成功");
            } else {
              this.$message.warning(res.msg);
            }
          });
        } else {
          console.log("请填入完整信息");
          return false;
        }
      });

    }
  }
};
</script>
<style lang="less" scoped>
</style>